package com.rocching.easychat.ui;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.rocching.easychat.R;
import com.rocching.easychat.core.UserIdentityContext;
import com.rocching.easychat.core.ViewClickListener;
import com.rocching.easychat.util.AppUtil;
import com.rocching.easychat.util.StringUtil;

public class FragmentMe extends Fragment {
    private final String TAG = FragmentMe.class.getName();

    public FragmentMe() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_me, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        init();
    }

    public void refreshData() {
        try {
            FragmentActivity activity = getActivity();
            if (activity != null) {
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        updateView();
                    }
                });
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    private void updateView() {
        View view = getView();
        if (view != null) {
            TextView userName = view.findViewById(R.id.user_name);
            TextView userAccount = view.findViewById(R.id.user_account);
            ImageView imageView = view.findViewById(R.id.user_photo);

            userName.setText(UserIdentityContext.getUserName());
            userAccount.setText(UserIdentityContext.getAccount());

            Uri url = UserIdentityContext.getPhotoUri();
            if (url != null) {
                Glide.with(view).load(url).placeholder(R.drawable.loading).into(imageView);
            }
        }
    }

    private void init() {
        initListener();
        updateView();
    }

    private void initListener() {
        View view = getView();
        if (view == null) {
            return;
        }

        View.OnClickListener defaultClickListener = new ViewClickListener() {
            @Override
            public void onDClick(View v) {
                Toast.makeText(getContext(), getText(R.string.no_function_tip), Toast.LENGTH_SHORT).show();
            }
        };

        LinearLayout personalLayout = view.findViewById(R.id.personal_layout);
        LinearLayout wePayLayout = view.findViewById(R.id.we_pay_layout);
        LinearLayout collectionLayout = view.findViewById(R.id.collection_layout);
        LinearLayout weMomentLayout = view.findViewById(R.id.we_moment_layout);
        LinearLayout cardBgLayout = view.findViewById(R.id.card_bg_layout);
        LinearLayout smileLayout = view.findViewById(R.id.smile_layout);
        LinearLayout configLayout = view.findViewById(R.id.config_layout);

        wePayLayout.setOnClickListener(defaultClickListener);
        collectionLayout.setOnClickListener(defaultClickListener);
        weMomentLayout.setOnClickListener(defaultClickListener);
        cardBgLayout.setOnClickListener(defaultClickListener);
        smileLayout.setOnClickListener(defaultClickListener);

        configLayout.setOnClickListener(new ViewClickListener() {
            @Override
            public void onDClick(View v) {
                Intent intent = new Intent(getContext(), FragmentMeConfigActivity.class);
                startActivity(intent);
            }
        });

        personalLayout.setOnClickListener(new ViewClickListener() {
            @Override
            public void onDClick(View v) {
                Intent intent = new Intent(getContext(), FragmentMeDetailActivity.class);
                startActivity(intent);
            }
        });
    }
}